package com.example.controller;


import com.example.controller.request.LinkGroupAddRequest;
import com.example.controller.request.LinkGroupUpdateRequest;
import com.example.enums.BizCodeEnum;
import com.example.model.LinkGroupDO;
import com.example.service.LinkGroupService;
import com.example.utils.JsonData;
import com.example.vo.LinkGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 短链分组的增删改查操作
 * @author yinxin
 * @since 2022-06-08
 */
@RestController
@RequestMapping("/api/group/v1")
public class LinkGroupController {

    @Autowired
    private LinkGroupService linkGroupService;

    /**
     * 创建分组
     * @param addRequest
     * @return
     */
    @PostMapping("/add")
    public JsonData add(@RequestBody LinkGroupAddRequest addRequest){
        int rows = linkGroupService.add(addRequest);
        return rows==1?JsonData.buildSuccess():JsonData.buildResult(BizCodeEnum.GROUP_ADD_FAIL);
    }


    /**
     * 根据id删除分组
     * @param groupId
     * @return
     */
    @DeleteMapping("/del/{group_id}")
    public JsonData del(@PathVariable("group_id") Long groupId){
        int rows = linkGroupService.del(groupId);
        return rows==1?JsonData.buildSuccess():JsonData.buildResult(BizCodeEnum.GROUP_NOT_EXIST);
    }

    /**
     * 根据id找详情
     * @param groupId
     * @return
     */
    @GetMapping("detail/{group_id}")
    public JsonData detail(@PathVariable("group_id") Long groupId){
        LinkGroupVO groupVO = linkGroupService.detail(groupId);
        return JsonData.buildSuccess(groupVO);
    }

    /**
     * 列出用户全部分组
     * @return
     */
    @GetMapping("/list")
    public JsonData findUserAllLinkGroup(){
        List<LinkGroupVO> list = linkGroupService.listAllGroup();
        return JsonData.buildSuccess(list);
    }


    /**
     * 跟新用户组
     * @return
     */
    @PostMapping("/update")
    public JsonData updateUserById(@RequestBody LinkGroupUpdateRequest updateRequest){
        int rows = linkGroupService.updateGroupById(updateRequest);
        return rows==1?JsonData.buildSuccess():JsonData.buildResult(BizCodeEnum.GROUP_OPER_FAIL);
    }
}

